package com.spic.business.plan.team.service.impl;

import com.spic.business.plan.team.domain.PlanTeam;
import com.spic.business.plan.team.mapper.PlanTeamMapper;
import com.spic.business.plan.team.service.IPlanTeamService;
import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.SecurityUtils;
import com.spic.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 应急救援队伍Service业务层处理
 *
 * @author spic
 * @date 2021-11-10
 */
@Service
public class PlanTeamServiceImpl implements IPlanTeamService
{
    @Autowired
    private PlanTeamMapper planTeamMapper;

    /**
     * 查询应急救援队伍
     *
     * @param id 应急救援队伍主键
     * @return 应急救援队伍
     */
    @Override
    public PlanTeam selectPlanTeamById(Long id)
    {
        return planTeamMapper.selectPlanTeamById(id);
    }

    /**
     * 查询应急救援队伍列表
     *
     * @param planTeam 应急救援队伍
     * @return 应急救援队伍
     */
    @Override
    public List<PlanTeam> selectPlanTeamList(PlanTeam planTeam)
    {
        List<PlanTeam> planTeams=new ArrayList<>();
        planTeams.addAll(selectPlanTeamList0(planTeam));
        planTeams.addAll(selectPlanTeamList1(planTeam));


        return planTeams;
    }
    /**
     * 查询部门级应急救援队伍列表
     *
     * @param planTeam 应急救援队伍
     * @return 应急救援队伍
     */
    @Override
    public List<PlanTeam> selectPlanTeamList1(PlanTeam planTeam)
    {
        planTeam.setDeptId(Long.valueOf(SecurityUtils.getDeptId()));
        return planTeamMapper.selectPlanTeamList1(planTeam);
    }
    /**
     * 查询矿级应急救援队伍列表
     *
     * @param planTeam 应急救援队伍
     * @return 应急救援队伍"
     */
    @Override
    public List<PlanTeam> selectPlanTeamList0(PlanTeam planTeam)
    {
        return planTeamMapper.selectPlanTeamList0(planTeam);
    }
    /**
     * 新增应急救援队伍
     *
     * @param planTeam 应急救援队伍
     * @return 结果
     */
    @Override
    public int insertPlanTeam(PlanTeam planTeam)
    {
        if("2".equals(planTeam.getPlanTeamLevel())){
            planTeam.setDeptId(Long.valueOf(SecurityUtils.getDeptId()));
        }
        planTeam.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        planTeam.setCreateTime(DateUtils.getNowDate());
        return planTeamMapper.insertPlanTeam(planTeam);
    }

    /**
     * 修改应急救援队伍
     *
     * @param planTeam 应急救援队伍
     * @return 结果
     */
    @Override
    public int updatePlanTeam(PlanTeam planTeam)
    {
        if("2".equals(planTeam.getPlanTeamLevel())){
            planTeam.setDeptId(Long.valueOf(SecurityUtils.getDeptId()));
        }
        return planTeamMapper.updatePlanTeam(planTeam);
    }

    /**
     * 批量删除应急救援队伍
     *
     * @param ids 需要删除的应急救援队伍主键
     * @return 结果
     */
    @Override
    public int deletePlanTeamByIds(Long[] ids)
    {
        return planTeamMapper.deletePlanTeamByIds(ids);
    }

    /**
     * 删除应急救援队伍信息
     *
     * @param id 应急救援队伍主键
     * @return 结果
     */
    @Override
    public int deletePlanTeamById(Long id)
    {
        return planTeamMapper.deletePlanTeamById(id);
    }
    /**
     * 校验队伍名称是否重复
     *
     * @param planTeam 应急队伍
     * @return 结果 0 不重复  1重复  2修改不重复 新增重复
     */
    public int checkName(PlanTeam planTeam){
        if(StringUtils.isNotEmpty(planTeam.getTeamName())) {
            if (planTeamMapper.selectByName(planTeam.getTeamName())!=null){
                if (!planTeamMapper.selectByName(planTeam.getTeamName()).getId().equals(planTeam.getId())){
                    return 1;
                }else{
                    return 2;
                }
            }else{
                return 0;
            }
        }else{
            return 0;
        }
    }
}
